package com.tnl.utils;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dyplsapi.model.v20170525.BindAxbRequest;
import com.aliyuncs.dyplsapi.model.v20170525.BindAxbResponse;
import com.aliyuncs.dyplsapi.model.v20170525.BindAxnExtensionResponse;
import com.aliyuncs.dyplsapi.model.v20170525.ReleaseSecretNoRequest;
import com.aliyuncs.dyplsapi.model.v20170525.ReleaseSecretNoResponse;
import com.aliyuncs.dyplsapi.model.v20170525.UnbindSubscriptionRequest;
import com.aliyuncs.dyplsapi.model.v20170525.UnbindSubscriptionResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.tnl.base.Const;

public class SecretAXB {
	
	/**
	 * 绑定AXB
	 * @param phoneNoA
	 * @param PhoneNoB
	 * @param invalidTime
	 * @return
	 * @throws ClientException
	 */
	 public static BindAxbResponse bindAxb(String phoneNoA,String PhoneNoB,String invalidTime) throws ClientException {

	        //可自助调整超时时间
	        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
	        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

	        //初始化acsClient,暂不支持region化
	        IClientProfile profile = DefaultProfile.getProfile(Const.regionId, Const.accessKeyId, Const.accessKeySecret);
	        DefaultProfile.addEndpoint(Const.regionId, Const.regionId, Const.product, Const.domain);
	        IAcsClient acsClient = new DefaultAcsClient(profile);

	        //组装请求对象-具体描述见控制台-文档部分内容
	        BindAxbRequest request = new BindAxbRequest();
	        //必填:对应的号池Key
	        request.setPoolKey(Const.poolKey);
	        //必填:AXB关系中的A号码
	        request.setPhoneNoA(phoneNoA);
	        //必填:AXB关系中的B号码
	        request.setPhoneNoB(PhoneNoB);
	        //必填:绑定关系对应的失效时间-不能早于当前系统时间
	        request.setExpiration(invalidTime);
	        //可选:是否需要录制音频-默认是false
	        request.setIsRecordingEnabled(false);

	        //外部业务自定义ID属性
	        request.setOutId(Const.outId);

	        //hint 此处可能会抛出异常，注意catch
	        BindAxbResponse response = acsClient.getAcsResponse(request);

	        return response;
	    }
	 
	  /**
	     * 订购关系解绑示例(解绑接口不区分AXB、AXN)
	     * @return
	     * @throws ClientException
	     */
	    public static UnbindSubscriptionResponse unbind(String subsId, String secretNo) throws ClientException {

	        //可自助调整超时时间
	        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
	        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

	        //初始化acsClient,暂不支持region化
	        IClientProfile profile = DefaultProfile.getProfile(Const.regionId, Const.accessKeyId, Const.accessKeySecret);
	        DefaultProfile.addEndpoint(Const.regionId, Const.regionId, Const.product, Const.domain);
	        IAcsClient acsClient = new DefaultAcsClient(profile);

	        //组装请求对象
	        UnbindSubscriptionRequest request = new UnbindSubscriptionRequest();
	        //必填:对应的号池Key
	        request.setPoolKey(Const.poolKey);
	        //必填-分配的X小号-对应到绑定接口中返回的secretNo;
	        request.setSecretNo(secretNo);
	        //可选-绑定关系对应的ID-对应到绑定接口中返回的subsId;
	        request.setSubsId(subsId);

	        UnbindSubscriptionResponse response = acsClient.getAcsResponse(request);

	        return response;
	    }
	 
	 	/**
	     * 订购关系解绑示例(解绑接口不区分AXB、AXN)
	     * @return
	     * @throws ClientException
	     */
	    public static ReleaseSecretNoResponse releaseSecretNo(String poolKey, String secretNo) throws ClientException {

	        //可自助调整超时时间
	        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
	        System.setProperty("sun.net.client.defaultReadTimeout", "10000");

	        //初始化acsClient,暂不支持region化
	        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", Const.accessKeyId,  Const.accessKeySecret);
	        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou",  Const.product,  Const.domain);
	        IAcsClient acsClient = new DefaultAcsClient(profile);

	        //组装请求对象
	        ReleaseSecretNoRequest request = new ReleaseSecretNoRequest();
	        //必填:对应的号池Key
	        request.setPoolKey(poolKey);
	        //必填-分配的X小号-对应到绑定接口中返回的secretNo;
	        request.setSecretNo(secretNo);

	        ReleaseSecretNoResponse response = acsClient.getAcsResponse(request);

	        return response;
	    }
	    
	    public static void main(String[] args) throws ClientException, InterruptedException {

	    	//隐私号
	    	String secretNo2 = "17521381014";
	        //AXB绑定
	        BindAxbResponse axbResponse = bindAxb("13584593495",secretNo2,"2020-07-10 16:40:00");
	        System.out.println("AXB绑定接口返回的结果----------------");
	        System.out.println("Code=" + axbResponse.getCode());
	        System.out.println("Message=" + axbResponse.getMessage());
	        System.out.println("RequestId=" + axbResponse.getRequestId());
	        String axbSubsId = axbResponse.getSecretBindDTO() == null ? null : axbResponse.getSecretBindDTO().getSubsId();
	        String axbSecretNo = axbResponse.getSecretBindDTO() == null ? null : axbResponse.getSecretBindDTO().getSecretNo();
	        System.out.println("subsId=" + axbSubsId);
	        System.out.println("secretNo=" + axbSecretNo);

	        //预留一分钟给您做拨打测试
	        Thread.sleep(2000L);

	        //解绑
	        if (axbResponse.getCode() != null && axbResponse.getCode().equals("OK")) {
	            UnbindSubscriptionResponse unbind = unbind(axbSubsId, axbSecretNo);
	            System.out.println("解绑接口返回数据AXB----------------");
	            System.out.println("Code=" + unbind.getCode());
	            System.out.println("Message=" + unbind.getMessage());
	            System.out.println("RequestId=" + unbind.getRequestId());
	        }
	        
	        //号码释放
	        String poolKey2 = Const.poolKey;
	        
	        ReleaseSecretNoResponse response2 = releaseSecretNo(poolKey2, secretNo2);
	        System.out.println("releaseSecretNo接口返回的结果----------------");
	        System.out.println("Code=" + response2.getCode());
	        System.out.println("Message=" + response2.getMessage());
	        System.out.println("RequestId=" + response2.getRequestId());
	        //号码购买
//	        String poolKey = "";
//	        Long specId = 3L;
//	        String city = "金华";
//	        String secretNo = "";
//	        BuySecretNoResponse response = buySecretNo(poolKey, specId, city, secretNo);
//	        System.out.println("buySecretNo接口返回的结果----------------");
//	        System.out.println("Code=" + response.getCode());
//	        System.out.println("Message=" + response.getMessage());
//	        System.out.println("RequestId=" + response.getRequestId());
//	        System.out.println("SecretNo=" + response.getSecretBuyInfoDTO().getSecretNo());

	   

	    }
}
